<html>
<head>
	<meta charset="UTF-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width" name="viewport">
	<title>3670：[Noi2014]动物园</title>
	<!-- css -->
	<link href="../css/base.min.css" rel="stylesheet">
	<link href="../css/project.min.css" rel="stylesheet">
	
	<!-- favicon -->
	<!-- ... -->
</head>
<body class="page-brand">
	<header class="header header-transparent header-waterfall ui-header">
		<ul class="nav nav-list pull-left">
			<li>
				<a data-toggle="menu" href="#menu">
					<span class="icon icon-lg">menu</span>
				</a>
			</li>
		</ul>
		<a class="header-logo header-affix-hide margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[Noi2014]动物园</a>
		<span class="header-logo header-affix margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">[Noi2014]动物园</span>
	</header>
	<nav aria-hidden="true" class="menu" id="menu" tabindex="-1">
		<div class="menu-scroll">
			<div class="menu-content">
				<a class="menu-logo" href="../index.html">BZOJ离线题库</a>
				<ul class="nav">
					<li>
						<a class="waves-attach" data-toggle="collapse" href="#problems">题目</a>
						<ul class="menu-collapse collapse in" id="problems">
							<li>
								<a class="waves-attach" href="../index.html">主页</a>
							</li>
							<li>
								<a class="waves-attach" href="../list.html">题目列表</a>
							</li>
						</ul>
					</li>
					<li>
						<a class="collapsed waves-attach" data-toggle="collapse" href="#about">关于</a>
						<ul class="menu-collapse collapse" id="about">
							<li>
								<a class="waves-attach" href="../about.html">关于此项目</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</div>
		</div>
	</nav>
	<main class="content">
		<div class="content-header ui-content-header">
			<div class="container">
				<h1 class="content-heading">
                [Noi2014]动物园                </h1>
                <p>时间限制：10s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  空间限制：512MB</p>			</div>
		</div>
		<div class="container">
			<section class="content-inner margin-top-no">
				<div class="row">
					<div class="col-lg-13 col-md-13">
						<div class="card margin-bottom-no">
							<div class="card-main">
								<div class="card-inner">
									
                                <h3>题目描述</h3><p><p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">近日，园长发现动物园中好吃懒做的动物越来越多了。例如企鹅，只会卖萌向游客要吃的。为了整治动物园的不良风气，让动物们凭自己的真才实学向游客要吃的，园长决定开设算法班，让动物们学习算法。</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">某天，园长给动物们讲解</span><span lang="EN-US">KMP</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">算法。</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">园长：&ldquo;对于一个字符串</span><span lang="EN-US">S</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，它的长度为</span><span lang="EN-US">L</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">。我们可以在</span><span lang="EN-US">O(L)</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">的时间内，求出一个名为</span><span lang="EN-US">next</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">的数组。有谁预习了</span><span lang="EN-US">next</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">数组的含义吗？&rdquo;</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">熊猫：&ldquo;对于字符串</span><span lang="EN-US">S</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">的前</span><span lang="EN-US">i</span></span><span lang="EN-US" style="font-size: 10.5pt; font-family: &quot;calibri&quot;,&quot;sans-serif&quot;; position: relative; top: 5.5pt; mso-bidi-font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'timesnewroman'; mso-bidi-theme-font: minor-bidi; mso-text-raise: -5.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t"></o:lock></v:path></v:stroke></v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width: 3pt; height: 18.75pt"><v:imagedata src="file:///C:%5CUsers%5C%E6%9C%8B%E9%A3%9E%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.png" o:title="" chromakey="white"></v:imagedata></v:shape></span><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">个字符构成的子串，既是它的后缀又是它的前缀的字符串中（它本身除外），最长的长度记作</span><span lang="EN-US"><font face="宋体, MS Song">next[i]</font></span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">。&rdquo;</span></span><font size="3"><span lang="EN-US"><o:p></o:p></span></font></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">园长：&ldquo;非常好！那你能举个例子吗？&rdquo;</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">熊猫：&ldquo;例</span><span lang="EN-US">S</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">为</span><u><span lang="EN-US">abcababc</span></u><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，则</span><span lang="EN-US">next[5]=2</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">。因为</span><span lang="EN-US">S</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">的前</span><span lang="EN-US">5</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">个字符为</span><u><span lang="EN-US">abcab</span></u><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，</span><u><span lang="EN-US">ab</span></u><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">既是它的后缀又是它的前缀，并且找不到一个更长的字符串满足这个性质。同理，还可得出</span><span lang="EN-US">next[1] = next[2] = next[3] = 0</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，</span><span lang="EN-US">next[4] = next[6] = 1</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，</span><span lang="EN-US">next[7] = 2</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">，</span><span lang="EN-US">next[8] = 3</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">。&rdquo;</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">园长表扬了认真预习的熊猫同学。随后，他详细讲解了如何在</span><span lang="EN-US">O(L)</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">的时间内求出</span><span lang="EN-US">next</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">数组。</span></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">下课前，园长提出了一个问题：&ldquo;</span><span lang="EN-US">KMP</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">算法只能求出<span lang="EN-US">next</span>数组。我现在希望求出</span><span style="line-height: 1.5; font-family: 宋体">一</span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">个更强大</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5">num</span></span><span lang="EN-US" style="font-size: 10.5pt; text-indent: 24pt; font-family: calibri, sans-serif; position: relative; top: 5.5pt"><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width: 3pt; height: 18.75pt"><v:imagedata src="file:///C:%5CUsers%5C%E6%9C%8B%E9%A3%9E%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.png" o:title="" chromakey="white"></v:imagedata></v:shape></span><span style="font-size: medium"><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">数组一一对于字符串S的前i个字符构成的子串，既是它的后缀同时又是它的前缀，并且该后缀与该前缀不重叠，将这种字符串的数量记作</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">num[i]</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">。例如</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">S</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">为</span><u style="text-indent: 24pt; line-height: 1.5"><span lang="EN-US"><font face="宋体, MS Song">aaaaa</font></span></u><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">，则</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">num[4] = 2</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">。这是因为</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">S</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">的前</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">4</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">个字符为</span><u style="text-indent: 24pt; line-height: 1.5"><span lang="EN-US"><font face="宋体, MS Song">aaaa</font></span></u><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">，其中</span><u style="text-indent: 24pt; line-height: 1.5"><span lang="EN-US"><font face="宋体, MS Song">a</font></span></u><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">和</span><u style="text-indent: 24pt; line-height: 1.5"><span lang="EN-US"><font face="宋体, MS Song">aa</font></span></u><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">都满足性质&lsquo;既是后缀又是前缀&rsquo;，同时保证这个后缀与这个前缀不重叠。而</span><u style="text-indent: 24pt; line-height: 1.5"><span lang="EN-US"><font face="宋体, MS Song">aaa</font></span></u><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">虽然满足性质&lsquo;既是后缀又是前缀&rsquo;，但遗憾的是这个后缀与这个前缀重叠了，所以不能计算在内。同理，</span><span lang="EN-US" style="text-indent: 24pt; line-height: 1.5"><font face="宋体, MS Song">num[1] = 0,num[2] = num[3] = 1,num[5] = 2</font></span><span style="text-indent: 24pt; line-height: 1.5; font-family: 宋体">。&rdquo;</span></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">最后，园长给出了奖励条件，第一个做对的同学奖励巧克力一盒。听了这句话，睡了一节课的企鹅立刻就醒过来了！但企鹅并不会做这道题，于是向参观动物园的你寻求帮助。你能否帮助企鹅写一个程序求出</span><span lang="EN-US">num</span></span><span lang="EN-US" style="font-size: 10.5pt; font-family: &quot;calibri&quot;,&quot;sans-serif&quot;; position: relative; top: 5.5pt; mso-bidi-font-size: 11.0pt; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'timesnewroman'; mso-bidi-theme-font: minor-bidi; mso-text-raise: -5.5pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width: 3pt; height: 18.75pt"><v:imagedata src="file:///C:%5CUsers%5C%E6%9C%8B%E9%A3%9E%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.png" o:title="" chromakey="white"></v:imagedata></v:shape></span><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">数组呢？</span></span><font size="3"><span lang="EN-US"><o:p></o:p></span></font></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'timesnewroman'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'timesnewroman'; mso-ansi-language: en-us; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt">特别地，为了避免大量的输出，你不需要输出</span><span lang="EN-US" style="font-family: &quot;timesnewroman&quot;,&quot;serif&quot;; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: 宋体; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt">num[i]</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'timesnewroman'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'timesnewroman'; mso-ansi-language: EN-US; mso-fareast-language: zh-cn; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt">分别是多少，你只需要输出<img height="36" alt="" width="144" src="../file/3670_0.jpg" /></span><span style="font-family: 宋体">对</span><span lang="EN-US" style="font-family: 'Times New Roman', serif">1,000,000,007</span></span><span lang="EN-US" style="font-size: 10.5pt; font-family: 'Times New Roman', serif; position: relative; top: 2.5pt"><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><o:lock v:ext="edit" aspectratio="t"></o:lock></v:path></v:stroke></v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width: 2.25pt; height: 12pt"><v:imagedata src="file:///C:%5CUsers%5C%E6%9C%8B%E9%A3%9E%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_image001.png" o:title="" chromakey="white"></v:imagedata></v:shape></span><span style="font-size: medium"><span style="font-family: 宋体">取模的结果即可。</span></span></p>
<p class="NOI" style="margin: 2.4pt 0cm; text-indent: 24pt"><span style="font-size: medium"><span style="font-family: 宋体"><img height="36" alt="" width="541" src="../file/3670_1.jpg" /></span></span></p></p><hr/><h3>输入格式</h3><p><p><span style="font-size: medium">第1行仅包含一个正整数n ，表示测试数据的组数。随后n行，每行描述一组测试数据。每组测试数据仅含有一个字符串S，S的定义详见题目描述。数据保证S 中仅含小写字母。输入文件中不会包含多余的空行，行末不会存在多余的空格。</span></p></p><hr/><h3>输出格式</h3><p><p><span style="font-size: medium">包含 n 行，每行描述一组测试数据的答案，答案的顺序应与输入数据的顺序保持一致。对于每组测试数据，仅需要输出一个整数，表示这组测试数据的答案对 1,000,000,007 取模的结果。输出文件中不应包含多余的空行。 </span></p></p><hr/><h3>样例输入</h3><pre>3
aaaaa
ab
abcababc</pre><hr/><h3>样例输出</h3><pre>36
1
32 </pre><hr/><h3>提示</h3><p><p><span lang="EN-US">n</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">&le;</span><span lang="EN-US">5,L</span><span style="font-family: 宋体; mso-ascii-font-family: 'timesnewroman'; mso-hansi-font-family: 'Times New Roman'">&le;</span><span lang="EN-US">1,000,000</span></p></p><hr/><h3>题目来源</h3><p>没有写明来源</p>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				
			</section>
		</div>
	</main>

	<div class="fbtn-container">
		<div class="fbtn-inner">
			<a class="fbtn fbtn-lg fbtn-brand-accent waves-attach waves-circle waves-light waves-effect" data-toggle="dropdown" aria-expanded="true"><span class="fbtn-text fbtn-text-left">Menu</span><span class="fbtn-ori icon">apps</span><span class="fbtn-sub icon">close</span></a>
			<div class="fbtn-dropup">
				<a class="fbtn fbtn-brand waves-attach waves-circle waves-light waves-effect" href="../list.html" target="_self"><span class="fbtn-text fbtn-text-left">题目列表</span><span class="icon">menu</span></a>
				<a class="fbtn fbtn-green waves-attach waves-circle waves-effect" href="../index.html" target="_self"><span class="fbtn-text fbtn-text-left">返回主页</span><span class="icon">home</span></a>
				<a class="fbtn waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/submitpage.php?id=3670" target="_blank"><span class="fbtn-text fbtn-text-left">提交代码</span><span class="icon">send</span></a>
				<a class="fbtn fbtn-orange waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=3670" target="_blank"><span class="fbtn-text fbtn-text-left">试题讨论</span><span class="icon">chat</span></a>
				
			</div>
		</div>
	</div>

	<!-- js -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/base.min.js"></script>
	<script src="../js/project.min.js"></script>
</body>
</html>